package com.vladimirkolarevic.eventplanner.tapestry.pages;

import java.util.List;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.RequestGlobals;

import com.vladimirkolarevic.eventplanner.City;
import com.vladimirkolarevic.eventplanner.DateAndTime;
import com.vladimirkolarevic.eventplanner.Event;
import com.vladimirkolarevic.eventplanner.Location;
import com.vladimirkolarevic.eventplanner.Performer;
import com.vladimirkolarevic.eventplanner.Place;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.CityBean;
import com.vladimirkolarevic.eventplanner.beans.DateAndTimeBean;
import com.vladimirkolarevic.eventplanner.beans.EventBean;
import com.vladimirkolarevic.eventplanner.beans.LocationBean;
import com.vladimirkolarevic.eventplanner.beans.PerformerBean;
import com.vladimirkolarevic.eventplanner.beans.PlaceBean;
import com.vladimirkolarevic.eventplanner.service.EventsService;
import com.vladimirkolarevic.eventplanner.service.MembershipsService;

@SuppressWarnings("deprecation")
public class AddEvent {

	@Component(id = "AddEventForm")
	private Form form;

	@ApplicationState
	private User user;
	private boolean userExists;

	@Property
	@Persist
	private Event event;

	@Property
	@Persist
	private DateAndTime date;

	@Property
	@Persist
	private City city;

	@Persist
	private Location location;

	@Property
	private boolean show;

	@Property
	@Persist
	private Performer performer;

	@Property
	@Persist
	private Place place;

	@Inject
	private EventsService events;

	@Inject
	private MembershipsService memberships;

	@ApplicationState
	private List<Performer> performers;

	@SuppressWarnings("unused")
	@Inject
	private RequestGlobals requestGlobals;

	private Performer addedPerformer;

	@InjectPage
	private Index index;

	@SetupRender
	public void createObject() {

		event = new EventBean();
		date = new DateAndTimeBean();
		city = new CityBean();
		location = new LocationBean();
		performer = new PerformerBean();
		place = new PlaceBean();

	}

	public Object onActivate() {
		if (userExists) {
			return null;
		}
		return index;
	}

	Object onSuccessFromAddEventForm() {

		city.setPlace(place);

		location.setCity(city);

		event.setPerformers(performers);

		event.setLocation(location);

		event.setDateAndTime(date);

		Long id1 = events.addEvent(event);

		Event e = events.retriveEvent(id1);

		User u = memberships.findUser(user.getId());

		u.addEvents(event);

		memberships.changeUserInfo(u);

		if (id1 != null) {

			performers.clear();
		}

		return this;
	}

	void onValidateFormFromAddEventForm() {
		System.out.println("blbabad");
		if (performers.isEmpty()) {

			form.recordError("you have to add at least one performer");

		}
	}

	Object onSuccessFromAddPerformer() {
		performers.add(performer);
		return this;
	}

	public List<Performer> getAllPerformers() {
		return performers;
	}

	public Performer getaddedPerformer() {
		return addedPerformer;
	}

	public void setAddedPerformer(Performer addedPerformer) {
		this.addedPerformer = addedPerformer;
	}
}
